/*     */ package com.zimbra.cs.filter.jsieve;
/*     */ 
/*     */ import com.zimbra.common.service.ServiceException;
/*     */ import com.zimbra.cs.filter.ZimbraMailAdapter;
/*     */ import com.zimbra.cs.filter.ZimbraSieveException;
/*     */ import com.zimbra.cs.index.SortBy;
/*     */ import com.zimbra.cs.mailbox.Conversation;
/*     */ import com.zimbra.cs.mailbox.Flag;
/*     */ import com.zimbra.cs.mailbox.Mailbox;
/*     */ import com.zimbra.cs.mailbox.Message;
/*     */ import com.zimbra.cs.mime.ParsedMessage;
/*     */ import java.util.Iterator;
/*     */ import java.util.List;
/*     */ import org.apache.jsieve.Argument;
/*     */ import org.apache.jsieve.Arguments;
/*     */ import org.apache.jsieve.ScriptCoordinate;
/*     */ import org.apache.jsieve.SieveContext;
/*     */ import org.apache.jsieve.StringListArgument;
/*     */ import org.apache.jsieve.TagArgument;
/*     */ import org.apache.jsieve.exception.SieveException;
/*     */ import org.apache.jsieve.mail.MailAdapter;
/*     */ import org.apache.jsieve.tests.AbstractTest;
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ public final class ConversationTest
/*     */   extends AbstractTest
/*     */ {
/*     */   private static enum Where
/*     */   {
/*     */     private Where() {}
/*     */     
/*  53 */     STARTED,  PARTICIPATED;
/*     */     
/*     */     static final String TAG = ":where";
/*     */      }
/*  57 */   private Where where = Where.STARTED;
/*     */   
/*     */   protected void validateArguments(Arguments args, SieveContext ctx) throws SieveException
/*     */   {
/*  61 */     Iterator<Argument> itr = args.getArgumentList().iterator();
/*  62 */     while (itr.hasNext()) {
/*  63 */       Argument arg = (Argument)itr.next();
/*  64 */       if ((arg instanceof TagArgument)) {
/*  65 */         TagArgument tag = (TagArgument)arg;
/*  66 */         if (tag.is(":where")) {
/*  67 */           if (itr.hasNext()) {
/*  68 */             arg = (Argument)itr.next();
/*  69 */             if ((arg instanceof StringListArgument)) {
/*  70 */               StringListArgument list = (StringListArgument)arg;
/*  71 */               if (list.getList().size() == 1) {
/*  72 */                 String value = (String)list.getList().get(0);
/*     */                 try {
/*  74 */                   this.where = Where.valueOf(value.toUpperCase());
/*     */                 } catch (IllegalArgumentException e) {
/*  76 */                   throw ctx.getCoordinate().syntaxException("Unknown where: " + value);
/*     */                 }
/*     */               } else {
/*  79 */                 throw ctx.getCoordinate().syntaxException("Too many where: " + list.getList());
/*     */               }
/*     */             } else {
/*  82 */               throw ctx.getCoordinate().syntaxException(":where is missing an argument");
/*     */             }
/*     */           } else {
/*  85 */             throw ctx.getCoordinate().syntaxException(":where is missing an argument");
/*     */           }
/*     */         } else {
/*  88 */           throw ctx.getCoordinate().syntaxException("Unknown tag: " + tag.getTag());
/*     */         }
/*     */       } else {
/*  91 */         throw ctx.getCoordinate().syntaxException("Unexpected argument: " + arg.getValue());
/*     */       }
/*     */     }
/*     */   }
/*     */   
/*     */   protected boolean executeBasic(MailAdapter mail, Arguments args, SieveContext ctx) throws SieveException
/*     */   {
/*  98 */     if (!(mail instanceof ZimbraMailAdapter)) {
/*  99 */       return false;
/*     */     }
/* 101 */     ZimbraMailAdapter adapter = (ZimbraMailAdapter)mail;
/* 102 */     Mailbox mbox = adapter.getMailbox();
/*     */     List<Conversation> convs;
/*     */     try {
/* 105 */       convs = mbox.lookupConversation(adapter.getParsedMessage());
/*     */     } catch (ServiceException e) {
/* 107 */       throw new ZimbraSieveException(e);
/*     */     }
/* 109 */     if (convs.isEmpty()) {
/* 110 */       return false;
/*     */     }
/* 112 */     switch (this.where) {
/*     */     case STARTED: 
/* 114 */       for (Conversation conv : convs) {
/* 115 */         if ((conv.getFlagBitmask() & Flag.BITMASK_FROM_ME) > 0) {
/*     */           try {
/* 117 */             List<Message> msgs = mbox.getMessagesByConversation(null, conv.getId(), SortBy.DATE_ASC, 1);
/* 118 */             if (!msgs.isEmpty()) {
/* 119 */               Message msg = (Message)msgs.get(0);
/*     */               
/*     */ 
/*     */ 
/* 123 */               if (((msg.getFlagBitmask() & Flag.BITMASK_FROM_ME) > 0) && (!ParsedMessage.isReply(((Message)msgs.get(0)).getSubject())))
/*     */               {
/* 125 */                 return true;
/*     */               }
/*     */             }
/*     */           } catch (ServiceException e) {
/* 129 */             throw new ZimbraSieveException(e);
/*     */           }
/*     */         }
/*     */       }
/* 133 */       break;
/*     */     case PARTICIPATED: 
/* 135 */       for (Conversation conv : convs) {
/* 136 */         if ((conv.getFlagBitmask() & Flag.BITMASK_FROM_ME) > 0) {
/* 137 */           return true;
/*     */         }
/*     */       }
/* 140 */       break;
/*     */     default: 
/* 142 */       if (!$assertionsDisabled) throw new AssertionError(this.where);
/*     */       break; }
/* 144 */     return false;
/*     */   }
/*     */ }


/* Location:              /home/mint/zimbrastore.jar!/com/zimbra/cs/filter/jsieve/ConversationTest.class
 * Java compiler version: 7 (51.0)
 * JD-Core Version:       0.7.1
 */